<?php 
	/**
	* 
	*/
	class GroupModel extends BaseModel
	{
		public function index($para1){
			$groups = $this->find_all($table = 'nd_nhom', $orderby = 'id DESC', $where = '', $cols = '*', $limit = ($para1-1)*ROWOFPAGE.','.ROWOFPAGE);
			$this->viewModel->set('groups',$groups);
			$this->viewModel->set("pageTitle","Danh sách nhóm");
			//đếm số lượng nhóm
			$countGroup=$this->count($table = 'nd_nhom');
			$this->viewModel->set("countGroup",$countGroup);
			// trang hiện tại
			$this->viewModel->set("pageCurrent",$para1);
			//lấy số người trong mỗi nhóm
			$quantityUsers = $this->find_all($table = 'nd_laysonguoi', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
			$this->viewModel->set("quantityUsers",$quantityUsers);
       		return $this->viewModel;
		}
		public function create($data,$both=''){
			if($both==''){
				$functions=$this->find_all($table = 'nd_chucnang', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
				if(!empty($data)){
	            //return id of item after insert to db
	    			return $this->save('nd_nhom',$data,'');
		    	}
	    	}
	    	else return $this->save('nd_nhomchucnang',$data,'');
	        //param var for view
	        $this->viewModel->set("pageTitle","Thêm nhóm");
	        $this->viewModel->set("functions",$functions);
	        return $this->viewModel;
		}
		public function edit($id, $data){
			//lây chưc năng tương ưng với nhóm quyền
			$functionGroup=$this->find_all($table = 'nd_laychucnang', $orderby = 'IdNhom DESC', $where = 'IdNhom='.$id, $cols = '*', $limit = '');
			$functions=$this->find_all($table = 'nd_chucnang', $orderby = 'id DESC', $where = '', $cols = '*', $limit = '');
	        //update item when have data
	        if(!empty($data)){
	            //return bool
	            return $this->save($table = 'nd_nhom', $data = $data, $where = 'id='.$id);
	        }

	        //get item detail by id
	        $group = $this->find($table='nd_nhom', $orderby = 'id DESC', $where = 'id='.$id, $cols = '*', $limit = '');
	        
	        //param var for view
	        $this->viewModel->set("pageTitle","Chỉnh sửa nhóm quyền");
	        $this->viewModel->set("group",$group);
	        $this->viewModel->set("functionGroup",$functionGroup);
	        $this->viewModel->set("functions",$functions);
	        return $this->viewModel;
    	}
	}
 ?>